//
//  IOTCAddTaskVC.m
//  Test3
//
//  Created by User-04 on 3/19/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "IOTCAddTaskVC.h"
#import "IOTCTableData.h"
#import "IOTCTask.h"
#import "IOTCPickDate.h"

@interface IOTCAddTaskVC ()
@property (retain, nonatomic) IBOutlet UITextField *textField;
@property IOTCTableData* data;
@property (retain, nonatomic) IBOutlet UISwitch *switchReminder;
@property (retain, nonatomic) IBOutlet UILabel *dateLabel;
@property (retain, nonatomic) IBOutlet UIButton *button;

//mas;dkma;dma;dma;ldmaqs;dmas;dlmasdl;ma;dma;dlmas;dlma;ms;dma;flamsk;

//jfjdfjkdjfkjdfkjd
@end

@implementation IOTCAddTaskVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.data = [IOTCTableData sharedInstance];
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.title = @"Add Task";
    self.dateLabel.text = @"";
    
    
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    if(self.data.dueDate==nil){
        self.dateLabel.text = @"";
        self.button.enabled = NO;
    }
    else{
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy/MM/dd"];
        NSString* srt = [dateFormatter stringFromDate:self.data.dueDate];
        self.dateLabel.text = [NSString stringWithFormat:@"%@",srt];
        [dateFormatter release];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)cancel:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    self.data.dueDate = nil;
}
- (IBAction)add:(id)sender {
    if ([self.textField.text isEqualToString:@""] || self.data.dueDate == nil) {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Empty field" message:@"Enter a task" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    else{
        IOTCTask* task = [[IOTCTask alloc]initWithText:self.textField.text andWithDueDate:self.data.dueDate];
        [self.data addTask:task];
        UILocalNotification* notification = [[UILocalNotification alloc]init];
        [notification setFireDate:[task getDate]];
        [notification setAlertBody:[task getText]];
        [notification setTimeZone:[NSTimeZone systemTimeZone]];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        [task release];
        [self.navigationController popViewControllerAnimated:YES];
        [notification release];
        self.data.dueDate = nil;
    }
}
- (IBAction)checkedYesRemind:(id)sender {
    if(self.switchReminder.on){
        [self pickDate];
        self.button.enabled = YES;
    }
    else{
        self.data.dueDate = nil;
        self.dateLabel.text = @"";
        self.button.enabled = NO;
    }
}
- (IBAction)datePressed:(id)sender {
    [self pickDate];
}

-(void) pickDate{
    IOTCPickDate* pd = [[IOTCPickDate alloc]initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:pd animated:YES];
    [pd release];
}
- (void)dealloc {
    [_textField release];
    [_switchReminder release];
    [_dateLabel release];
    [_button release];
    [super dealloc];
}
       
@end
